package com.metel.mservice.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MsgUtil {

	private static Pattern scriptPattern = Pattern.compile("<(/){0,1}(script)([^>|[.]]*)?>?", Pattern.CASE_INSENSITIVE);
	private static Pattern objectPattern = Pattern.compile("<(/){0,1}(object)([^>|[.]]*)?>?", Pattern.CASE_INSENSITIVE);
	private static Pattern embedPattern = Pattern.compile("<(/){0,1}(embed)([^>|[.]]*)?>?", Pattern.CASE_INSENSITIVE);

	public static String convertXSSTag(String msg) {
		Matcher matcher = scriptPattern.matcher(msg);
		while (matcher.find()) {
			msg = msg.replace(matcher.group(0), matcher.group(0).replace(matcher.group(2), "!" + matcher.group(2)));
		}

		// 2. <object> 태그를 포함하면
		matcher = objectPattern.matcher(msg);
		while (matcher.find()) {
			msg = msg.replace(matcher.group(0), matcher.group(0).replace(matcher.group(2), "!" + matcher.group(2)));
		}

		// 3. <embed> 태그를 포함하면
		matcher = embedPattern.matcher(msg);
		while (matcher.find()) {
			msg = msg.replace(matcher.group(0), matcher.group(0).replace(matcher.group(2), "!" + matcher.group(2)));
		}

		return msg;
	}	
}
